package com.aaa.controller;

import com.aaa.dto.AddOrderDto;
import com.aaa.dto.OrderDetailDto;
import com.aaa.dto.OrderDto;
import com.aaa.pojo.DefaultMsg;
import com.aaa.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 跟据用户id查询该用户的订单简要数据
     *
     * @param userId 用户id
     * @return 返回所有订单的订单简要数据的集合
     */
    @PostMapping("/findOrderByUserId")
    public List<OrderDto> findOrderByUserId(Integer userId) {
        return orderService.findOrderByUserId(userId);
    }

    /**
     * 跟据订单id查询该订单的订单详细数据
     *
     * @param orderId 订单id
     * @return 返回该订单的详细数据的集合
     */
    @PostMapping("/findOrderDetailByOrderId")
    public List<OrderDetailDto> findOrderDetailByOrderId(Integer orderId) {
        return orderService.findOrderDetailByOrderId(orderId);
    }

    /**
     * 订单在限定时间内未支付，调用此方法使订单状态无效
     *
     * @param orderId 订单id
     * @return 返回是否成功的信息
     */
    @PostMapping("/updateStatusByOrderId")
    public DefaultMsg updateStatusByOrderId(Integer orderId) {
        DefaultMsg defaultMsg = new DefaultMsg(200, 1, null, null);
        int i = orderService.updateStatusByOrderId(orderId);
        if (i == 0) defaultMsg.setMsg(0);

        return defaultMsg;
    }

    /**
     * 用户删除订单，调用此方法
     *
     * @param orderId 订单id
     * @return 返回是否成功的信息
     */
    @PostMapping("/deleteOrderByOrderId")
    public DefaultMsg deleteOrderByOrderId(Integer orderId) {
        DefaultMsg defaultMsg = new DefaultMsg(200, 1, null, null);
        int i = orderService.deleteOrderByOrderId(orderId);
        if (i == 0) defaultMsg.setMsg(0);

        return defaultMsg;
    }

    @PostMapping("/updateStatusByOrderId_4")
    public DefaultMsg updateStatusByOrderId_4(Integer orderId) {
        return orderService.updateStatusByOrderId_4(orderId);
    }

    @PostMapping("/updateStatusByOrderId_1")
    public DefaultMsg updateStatusByOrderId_1(Integer orderId) {
        return orderService.updateStatusByOrderId_1(orderId);
    }

    @PostMapping("/addOrderByUserId")
    public DefaultMsg addOrderByUserId(@RequestBody AddOrderDto addOrderDto) {
        System.out.println(addOrderDto);
        return orderService.addOrderByUserId(addOrderDto);
    }

}
